const {
  CurrencyShopModel
} = require('../../../utils/currencyshop-model.js')
const utils =
  require("../../../utils/util.js")
const currencyShopModel = new CurrencyShopModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetId: '',
    coupons: [],
    selectCoupon: '',
    selectCouponType: true
  },

  goBindCoupon() {
    wx.navigateTo({
      url: '/pages/currencyShop/bindCoupon/index',
    })
  },

  selectCoupon(e) {
    const {
      index
    } = e.currentTarget.dataset
    let assetId = e.currentTarget.dataset.id
    var that = this
    if (assetId != that.data.assetId) {
      that.setData({
        assetId: assetId,
        selectCoupon: that.data.coupons[index]
      })
    } else {
      that.setData({
        assetId: "",
        selectCoupon: ''
      })
    }

    // wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const shopId = options.shopId
    const item = JSON.parse(options.items)
    const merchantId = wx.getStorageSync('merchantId')
    let request
    if (options.type == '1') {
      request = currencyShopModel.orderUnionCouponQuery({
        merchantId,
        shopId,
        items: [item],
        payPlatform: 'WECHAT_MALL',
        busType: 'CASH_GAME_COUPON',
        bizCode: '1161030'
      })
    } else if (options.type == '2') {
      request = currencyShopModel.orderUnionCouponQuery({
        merchantId,
        shopId,
        items: [item],
        payPlatform: 'WECHAT_MALL',
        busType: 'CASH_PKG_GAME_COUPON',
        bizCode: '1131035'
      })
    } else if (options.orderType && options.orderType == "mobile") {
      request = currencyShopModel.getMobileCouponList({
        merchantId,
        shopId,
        items: [item],
        payPlatform: 'WECHAT_MALL',
        busType: options.busType || "",
        operatorId: options.operatorId || "",
        operatorName: options.operatorName || "",
      })
    } else if (options.orderType && options.orderType == "newOrder") {
      request = currencyShopModel.getCouponListv2({
        merchantId,
        shopId,
        productList: [item],
        busType: options.busType || "",
      })
    } else {
      request = currencyShopModel.getCouponList({
        merchantId,
        shopId,
        items: [item],
        payPlatform: 'WECHAT_MALL'
      })
    }
    request.then(data => {
      let index = -1
      const coupons = (data || []).map((item, idx) => {
        let promotionAmount = ''
        if (item.couponType == 0) {
          promotionAmount = item.promotionAmount ? utils.formatCurrency(item.promotionAmount, 2) : '0.00'
        } else if (item.couponType == 1) {
          promotionAmount = (item.promotionAmount || 0).toFixed(1)
        } else if (item.couponType == 2) {
          promotionAmount = (item.promotionAmount || 0).toFixed(0)
        }
        if (options.assetId === item.assetId) {
          index = idx
        }
        const split = promotionAmount.split('.')
        return {
          refId: item.refId,
          assetId: item.assetId,
          couponName: item.couponName || '-',
          validBegin: item.validBegin ? item.validBegin.replace(/-/g, "/").substr(0, 10) : '',
          validEnd: item.validEnd ? item.validEnd.replace(/-/g, "/").substr(0, 10) : '',
          promotionAmount,
          integer: split[0],
          decimals: split[1],
          couponType: item.couponType || '',
          accumAmount: item.accumAmount || 0,
          maxAmount: item.maxAmount || 0,
          discountLimit: item.discountLimit || 0,
          assetStatus: item.assetStatus || '', //资产状态 0:未使用 1:已使用 2:已过期
          channels: item.channels || '', //渠道
          memberId: item.memberId || '',
          svipType: item.svipType, //SVIP类型 1:是 0:否
          couponId:item.couponId || '',
          couponCode:item.couponCode || '',
        }
      })
      this.setData({
        assetId: options.assetId,
        coupons,
        selectCoupon: coupons[index] || ''
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    const pages = getCurrentPages()
    const prevPage = pages[pages.length - 2]
    prevPage.setData({
      selectCouponType: this.data.selectCouponType,
      selectCoupon: this.data.selectCoupon
    }, () => {
      prevPage.getOrderInfo()
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})